package com.kjj.market.service.impl;

import com.kjj.market.domain.Business;
import com.kjj.market.domain.BusinessProduct;
import com.kjj.market.domain.Clue;
import com.kjj.market.domain.ClueRemark;
import com.kjj.market.dto.ClueActivityDto;
import com.kjj.market.dto.ClueBusinessDto;
import com.kjj.market.mapper.BusinessMapper;
import com.kjj.market.mapper.BusinessProductMapper;
import com.kjj.market.mapper.ClueMapper;
import com.kjj.market.mapper.ClueRemarkMapper;
import com.kjj.market.service.IClueService;
import com.kjj.basic.service.impl.BaseServiceImpl;
import com.kjj.org.domain.Employee;
import com.kjj.product.domain.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kan
 * @since 2022-08-27
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessProductMapper businessProductMapper;
    @Override
    public void bindActivitys(ClueActivityDto clueActivityDto) {
        Long clueId = clueActivityDto.getClueId();
        List<Long> activityIds = clueActivityDto.getActivityIds();
        List<Map<String, Long>> list = new ArrayList<>();
        activityIds.forEach(activityId->{
            Map<String, Long> map = new HashMap<>();
            map.put("clueId", clueId);
            map.put("activityId", activityId);
            list.add(map);
        });
        clueMapper.batchClueActivity(list);
    }

    @Transactional
    @Override
    public void assign(Clue clue) {
        clue.setState(1);
        clueMapper.update(clue);
    }

    @Transactional
    @Override
    public void clueSwitchBusiness(ClueBusinessDto clueBusinessDto) {
        Clue clue = clueBusinessDto.getClue();
        Product product = clueBusinessDto.getProduct();
        Employee seller = clueBusinessDto.getSeller();
        //修改线索状态
        clue.setSeller(seller);
        clue.setState(2);
        clueMapper.update(clue);
        //线索备注
        ClueRemark cr = new ClueRemark();
        cr.setNoteContent("已转化为商机，操作人员："+seller.getUsername());
        cr.setCreateTime(new Date());
        cr.setCreateBy(seller.getId());
        cr.setClueId(clue.getId());
        clueRemarkMapper.save(cr);
        //添加商机，-选择一个产品
        Business business = new Business();
        business.setName(clue.getFullName()+"意向购买"+product.getName());
        business.setClueId(clue.getId());
        business.setProductId(product.getId());
        business.setProductName(product.getName());
        business.setProductPrice(product.getPrice());
        businessMapper.save(business);
        //添加线索产品
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product,businessProduct);
        businessProduct.setId(null); //已经拷贝了id，所以要置空
        businessProduct.setBusinessId(business.getId());
        businessProductMapper.save(businessProduct);
    }
}
